<?php

namespace libraries;

use enums\HttpServerEnums;
class CookiesSwoole implements \Phalcon\Http\Response\CookiesInterface, \Phalcon\DI\InjectionAwareInterface
{
	private $_di = null;
	
	private $_useEncryption = false;
	
	private $_crypt = null;
	/*  
	 * @see \Phalcon\Http\Response\CookiesInterface::useEncryption()
	 */
	public function useEncryption( $useEncryption ) 
	{
		$this->_useEncryption = $useEncryption;
	}

	/*
	 * @see \Phalcon\Http\Response\CookiesInterface::isUsingEncryption()
	 */
	public function isUsingEncryption() 
	{
		return $this->_useEncryption;			
	}

	/* 
	 * @see \Phalcon\Http\Response\CookiesInterface::set()
	 */
	public function set( $name, $value = null, $expire = 0, $path = "/", $secure = null, $domain = null, $httpOnly = null) 
	{
		$strValue = $value;
		if( $this->_useEncryption && ( $name != HttpServerEnums::SESSION_KEY ))
		{
			if( empty( $this->_crypt ))
			{
				$this->_crypt = $this->_di->get( 'crypt' );
			}
				
			$strValue = $this->_crypt->encryptBase64( $value );
		}
		
		if( $name == HttpServerEnums::SESSION_KEY )
		{
			$httpOnly = true;
		}
		
		if( \HttpServer::$response->cookie( $name, $strValue, $expire, $path, $domain, $secure, $httpOnly ))
		{
			$cookie = new \Phalcon\Http\Cookie( $name );
			$cookie->useEncryption( $this->_useEncryption );
			$cookie->setValue( $value );
			
			return $cookie;
		}
		
		return false;
	}

	/* 
	 * @see \Phalcon\Http\Response\CookiesInterface::get()
	 */
	public function get( $name ) 
	{
		if( isset( $_COOKIE[ $name ] ) )
		{
			$cookie = new \Phalcon\Http\Cookie( $name );
			
			if( $this->_useEncryption && ( $name != HttpServerEnums::SESSION_KEY ))
			{
				if( empty( $this->_crypt ))
				{
					$this->_crypt = $this->_di->get( 'crypt' );
				}
				
// 				$cookie->useEncryption( true );
// 				$cookie->setDI( $this->_di );

				$cookie->setValue( $this->_crypt->decryptBase64( $_COOKIE[ $name ] ) );
			}
			
			return $cookie;
		}
		
		
		return null;
	}

	/* 
	 * @see \Phalcon\Http\Response\CookiesInterface::has()
	 */
	public function has($name) 
	{
		if( isset( $_COOKIE[ $name ] ) )
		{
			return true;
		}
		
		return false;
		
	}

	/*  
	 * @see \Phalcon\Http\Response\CookiesInterface::delete()
	 */
	public function delete($name) 
	{
		unset( $_COOKIE[ $name ] );
		
		return \HttpServer::$response->cookie( $name, '', TimeUtils::getIntTime() - 3600 );
		
// 		return setcookie( $name, '', $_SERVER[ 'REQUEST_TIME' ] - 3600 );
	}

	/*  
	 * @see \Phalcon\Http\Response\CookiesInterface::send()
	 */
	public function send() 
	{
		return true;
	}

	/*  
	 * @see \Phalcon\Http\Response\CookiesInterface::reset()
	 */
	public function reset() 
	{
		return true;
	}

	/*  
	 * @see \Phalcon\Di\InjectionAwareInterface::setDI()
	 */
	public function setDI(\Phalcon\DiInterface $dependencyInjector) 
	{
		// TODO Auto-generated method stub
		$this->_di = $dependencyInjector;
	}

	/*  
	 * @see \Phalcon\Di\InjectionAwareInterface::getDI()
	 */
	public function getDI() 
	{
		// TODO Auto-generated method stub
		return $this->_di;
	}

	
}